{% extends "ejeje/details-journalpost.html" %}


{%block title%}Be om innsyn i &laquo;{{object.tittel|title}}&raquo;{%endblock%}

{%block head%}

<!--script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.pack.js" /script -->

{%endblock%}

{%block script%}

function sendrequest(uri, data) {
  // send request to see a document, with return details in 'data'
  console.debug(uri.substring(0,11));
  if(uri.substring(0, 11) != '/innsyn/ny/') {
    return false;
  }
  var postdata = data || {};
  postdata.makerequest = 1;
  postdata.timestamp = (new Date()).getTime();
  try {
    var ret = postdata.username;
  } catch(e) {
    try {
      var ret = postdata.email;
    } catch(e) {
      return false;
    }
  }
  console.debug(postdata);
  console.debug(postdata['email']);
  $.post(uri, postdata,
         function(data, textStatus, xhr) { $.facebox(data); }
        );
}
var userdata = {'username':'{{req.user.username}}'};
$(function() {
  $("#authenticatedrequest").click(
    function(event) { 
      var docid = $("#document-id").val();
      $("#anonymousrequest").removeAttr("disabled");
      sendrequest('/innsyn/ny/'+docid, {'username':userdata.username});
            
    });
  $("#anonymousrequest").click(
    function(event) { 
      $(this).attr("disabled", "disabled");
      $("#loginrequest").removeAttr("disabled").hide();
      $("#anonymousrequest-form").show('fast');
    });
  var anonymousrequest_send = $("#anonymousrequest-send");
  anonymousrequest_send.click(
    function(event) { 
      var email = $("#anonymousrequest-email").val();
      var docid = $("#document-id").val();
      $("#anonymousrequest").removeAttr("disabled");
      $("#anonymousrequest-form").hide('fast');
      sendrequest('/innsyn/ny/'+docid, {'email':email});
            
    });
  $("#anonymousrequest-email").keypress(
    function(event) { 
     ($(this).val().length > 0) ? 
        anonymousrequest_send.removeAttr("disabled") :
        anonymousrequest_send.attr("disabled", "disabled");
    });
  $("#anonymousrequest-cancel").click(
    function(event) { 
      $("#anonymousrequest").removeAttr("disabled");
      $("#anonymousrequest-form").hide('fast');
      $("#loginrequest").show();
    });    
  $("#loginrequest").click(
    function(event) { 
      $(this).attr("disabled", "disabled");
      $("#anonymousrequest").removeAttr("disabled").hide();
      $("#loginrequest-form").show('fast');
    });
  $("#loginrequest-cancel").click(
    function(event) { 
      $("#loginrequest").removeAttr("disabled");
      $("#loginrequest-form").hide('fast');
      $("#anonymousrequest").show();
    });
  $("#loginrequest-send").click(
    function(event) { 
      var username = $("#loginrequest-username").val();
      var password = $("#loginrequest-password").val();
      if(!(username.length && password.length)) {
        $.facebox("Username and password!!");
        return false;
      }
      $.post('/innlogging.json', 
             {'username':username, 'password':password},
             function(data, textStatus, xhr) {
               // logged in
               $("body").trigger("authenticated", [ true ]);
               var docid = $("#document-id").val();
               sendrequest('/innsyn/ny/'+docid, {'username':username});

             });
    });
  
  $("#more-details").hide();
  $("#more-details-toggle").toggle(
    function(ev) { 
      $("#more-details").show("fast");  
      $(this).html("&#8854;");
    },
    function(ev) { 
      $("#more-details").hide("fast");
      $(this).html("&#8853;");
    }
  );
  $("body").bind("authenticated", function(event, bool) { window.location.reload(); });        
});

{%endblock%}

{%block preamble %}
<div id="requestdoc">
  {% if not object.uri %}
  <p class="note">
    <b>
    {% if object.offentlig %}
    Dokumentet er offentlig, men ikke tilgjengelig på nett. 
    {% else %}
    Dokumentet er unntatt offentlighet, men du kan likevel be om å få se det.
    {% endif %}
    </b>
    Her kan du sende en melding til kommunen og begjære innsyn etter offentlighetsloven.
  </p>
  <p><div id="feedback"></div>
   <input type="hidden" id="document-id" value="{{object.id}}">
  {% if req.user.is_authenticated %}
    Du er logget inn som <b>{{req.user.username}}</b><br>
    <button id="authenticatedrequest">Be om innsyn i dette dokumentet</button>
    <!--button id="sendanonrequest" >Be om innsyn uten å registrere det</button-->
  {% else %}
    <button id="anonymousrequest">Be om innsyn uten å logge inn</button>
    <button id="loginrequest">Logg inn og be om innsyn</button>
  {% endif %}
  </p>
  <div id="anonymousrequest-form" style="display:none">
     <label>Din epostadresse: 
        <input type="text" id="anonymousrequest-email" size="25" title="Dette er adressen dokumentet vil bli sendt til. Lagres ikke av oss">
     </label>
     <button id="anonymousrequest-send" disabled="disabled">Spør etter dokumentet</button>
     <button id="anonymousrequest-cancel">Avbryt</button>
  </div>
  <div id="loginrequest-form" style="display:none">
     <label for="loginrequest-email">Ditt brukernavn: </label><br>
     <input type="text" id="loginrequest-username" size="25">
     <br>
     <label for="loginrequest-password">Passord:</label><br>
     <input type="password" id="loginrequest-password" size="25">
     <br>
     <button id="loginrequest-send">Spør etter dokumentet</button>
     <button id="loginrequest-cancel">Avbryt</button>
  </div>
  {% else %}
  <p class="detail">Dokumentet er offentlig, og tilgjengelig på nett.
  <a href="{{object.uri|urlencode}}" rel="nofollow">Les &laquo;{{object.tittel|title}}&raquo;</a>
  </p>
  {% endif %}
</div>
<h3>Detaljer om dokumentet:</h3>
<span id="more-details-toggle">&#8853;</span>
{% endblock %}


